
//布局东
$(function () {

    //定义常用easyui对象
    var giftChoose_datagrid = $("#giftChoose_datagrid");
    var giftChoose_dialog = $("#giftChoose_dialog");
    //end

    //初始化礼品列表 gift_datagrid
    giftChoose_datagrid.datagrid({
        url:'/gift/list',
        fitColumns:true,
        fit:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        striped:true,
        toolbar: '#tb',
        columns:[[
            {field:"x",checkbox:true},//显示多选样式
            {field:'name',title:'礼品名称',width:100},
            {field:'points',title:'所需积分',width:100,align:'right'},
            {field:'number',title:'礼品数量',width:100,align:'right'},
            {field:'unit',title:'礼品单位',width:100,align:'right'}
        ]],
        //礼品弹框点击事件
        onClickRow:function (index,row) {
            $('#dg').datagrid("appendRow",row);
            giftChoose_dialog.dialog("close");
            //end

        }

    });
    //end
    //初始化保存或编辑对话框
    giftChoose_dialog.dialog({
        width: 800,
        height: 500,
        closed: true,
        buttons:"#bb"
    });

    $('#dg').datagrid({
        fitColumns:true,
        //url:'datagrid_data.json',
        columns:[[
            {field:'name',title:'礼品名称',width:100},
            {field:'points',title:'兑换积分',width:100},
            {field:'number',title:'剩余量',width:100}
        ]]
    });

    //响应集合函数对象
    var cmdObj = {
        //东布局礼品选择弹出对话框
        chooseGift: function () {
            //添加与编辑时将数据清空
            giftChoose_dialog.dialog("open");
            //giftChoose_dialog.datagrid("reload");
            giftChoose_dialog.dialog("setTitle","选择礼品");
        },
        remove:function () {

            var rows = $('#dg').datagrid('getRows');
            for (var i=rows.length;i>0;i--){
                $('#dg').datagrid('deleteRow',i-1);
            }
        },

        //马上兑换礼品
        exchange : function () {
           var row =  $('#dg').datagrid("getRows");
           if(!$("#member_datagrid").datagrid("getSelected")){
               $.messager.alert('温馨提示','请先选择会员');
           }else {
               var points = 0;
               for (var i = 0; i < row.length; i++) {
                   points = points + row[i].points;
                    var giftName = row[i].name;
                    var giftIntegral = row[i].points;

                   if(row.length == 0){
                       $.messager.alert('温馨提示','请先选择行');
                       return;
                   }
                   if($("#member_datagrid").datagrid("getSelected").integral < points){
                       $.messager.alert('温馨提示','积分不足');
                       return;
                   }else{


                       //修改会员积分
                       var memberRow = $("#member_datagrid").datagrid("getSelected");
                       var count = $("#count").numberspinner("getValue");
                       var number = 0 - points*count;
                       var memId = memberRow.id;
                       //ajax请求
                       $.post("/member/integralUpdate?",{id:memId,number:number},function (data) {
                           if(!data.success){
                               $.messager.alert('温馨提示',data.msg);
                           }else{
                               gift_datagrid.datagrid("load");
                           }
                       })

                       //更新兑换记录表
                       var exchangegiftRows = $("#exchangegift_datagrid").datagrid("getRows");
                       var exchangegiftRow = exchangegiftRows[0];
                       var rows= $("#exchangegift_datagrid").datagrid("getRows");
                       var ChangeRow = rows[0];
                        var memberinfoPoints = memberRow.integral;
                        var memberinfoName = memberRow.memberName;
                       var memberinfoCardnumber= memberRow.memberNumber;
                      ///南布局会员积分表对象
                       $.post("/exchangegift/save",{
                           "giftName":giftName,
                           "giftIntegral":giftIntegral,
                           "memberinfoName":memberinfoName,
                           "memberinfoCardnumber":memberRow.memberNumber,
                           "number":count,
                           "memberinfoPoints":memberinfoPoints


                       },function (data) {
                           if(data.success){
                               $.messager.alert('温馨提示','兑换成功');
                               $("#member_datagrid").datagrid("reload");
                               $("#exchangegift_datagrid").datagrid("reload");
                           }else {
                               $.messager.alert('温馨提示',data.msg);
                           }


                       });


                   }
               }
           }



        },

        //弹出礼品表高级查询
        queryForGift:function () {
            //获取高级查询输入框的文本
            var kw = $("#gKeyword").textbox("getValue")
            if(!kw){
                $.messager.alert('温馨提示','请输入关键字');
                return;
            }
            $("#giftChoose_datagrid").datagrid("load",{
                keyword:kw
            })
        }
        //end
    };

    //调用
    $("a[data-cmd]").click(function(){

        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
    //end

});

//布局中
$(function () {
    //定义常用easyui对象
    var member_datagrid = $("#member_datagrid");
    //end

    //初始化礼品列表 gift_datagrid
    member_datagrid.datagrid({
        title:"会员列表",
        url:'/member/list',
        fitColumns:true,
        fit:true,
        rownumbers:true,
        singleSelect:true,
        striped:true,
        toolbar: '#tm',
        columns:[[
            {field:'memberName',title:'会员名',width:100},
            {field:'balance',title:'储值',width:100,align:'right'},
            {field:'tel',title:'电话',width:100,align:'right'},
            {field:'grade',title:'等级',width:100,align:'right',formatter: function(value,row,index){
            if(value){
                return value.name;
            }else{
                return "无等级";
            }

            }
            }
        ]],

        //数据加载完成之后
        onLoadSuccess:function () {
            var rows = $("#member_datagrid").datagrid("getRows");


            //西布局会员详细信息列表
            $('#pg').propertygrid({
                showGroup: false,
                scrollbarSize: 0,
                fit:true,
                data: [
                    {"name":"会员姓名","value":rows[0].memberName,"editor":"text"},
                    {"name":"会员等级","value":rows[0].grade.name,"editor":"text"},
                    {"name":"当前积分","value":rows[0].integral,"editor":"text"},
                    {"name":"会员卡号","value":rows[0].memberNumber,"editor":"text"},
                    {"name":"会员余额","value":rows[0].balance,"editor":"text"},
                    {"name":"会员生日","value":rows[0].birthDate,"editor":"text"}

                ]

            });


        },

        onClickRow:function (index,row) {

        //会员点击事件西布局显示详细信息
            $('#pg').propertygrid({
                showGroup: false,
                scrollbarSize: 0,
                fit:true,
                data: [
                    {"name":"会员姓名","value":row.memberName,"editor":"text"},
                    {"name":"会员等级","value":row.grade.name,"editor":"text"},
                    {"name":"当前积分","value":row.integral,"editor":"text"},
                    {"name":"会员卡号","value":row.memberNumber,"editor":"text"},
                    {"name":"会员余额","value":row.balance,"editor":"text"},
                    {"name":"会员生日","value":row.birthDate,"editor":"text"}

                ]

            });

            //会员信息点击事件南布局列表高级查询

                //获取高级查询输入框的文本
                var kw = row.memberNumber;
                if(!kw){

                    return;
                }
            $("#exchangegift_datagrid").datagrid("load",{
                    keyword:kw
                });


            //end

        }
    });
    //end


});

//中间布局会员表高级查询
function query () {
    //获取高级查询输入框的文本
    var kw = $("#keyword").textbox("getValue")
    if(!kw){
        $.messager.alert('温馨提示','请输入关键字');
        return;
    }
    member_datagrid.datagrid("load",{
        keyword:kw
    })
}
//end

//布局南
$(function () {
    //定义常用easyui对象
    var exchangegift_datagrid = $("#exchangegift_datagrid");

    //end

    //初始化礼品兑换列表 exchangegift_datagrid
    exchangegift_datagrid.datagrid({

        url:'/exchangegift/list',
        fitColumns:true,
        fit:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        striped:true,
        toolbar: '#tb',
        columns:[[
            {field:'id',title:'编号',width:100},
            {field:'memberinfoCardnumber',title:'会员卡号',width:100},
            {field:'memberinfoName',title:'会员名称',width:100,align:'right'},
            {field:'giftName',title:'礼品名称',width:100,align:'right'},
            {field:'number',title:'兑换数量',width:100,align:'right'},
            {field:'giftIntegral',title:'消费积分',width:100,align:'right'},
            {field:'inputuserUsername',title:'操作人员',width:100,align:'right'},
            {field:'exchangedate',title:'兑换日期',width:100,align:'right'}
        ]]
    });
    //end





})





























